<template>
	<view class="user">
		<view class="top">
			<image class="topbg" src="/static/index/userbg.jpg" mode="widthFix"></image>
			<view class="infobox">
				<view class="info-t">
					<image :src="info.head_url ||'../../static/index/head.png'" @click="updateHead" mode=""
						class="header_logo">
					</image>
					<view class="userinfo">
						<button class="title" @click="showLogin">
							<text class="name" v-if="is_login">{{info.name || '游客'}}</text>
							<text class="name" v-else>登录/注册</text>
							<view class="level" @click="toPage('/pages/usersetup/index')">
								<uni-icons type="gear" size="28"></uni-icons>
							</view>
						</button>
						<view class="phone" v-if="is_login">
							{{indexInfo.level_name || ''}}
						</view>
						<view class="phone" v-if="is_login">
							{{info.phone || ''}}
						</view>
						<view class="phone" v-else>
							点击授权登录
						</view>
					</view>
				</view>
				<view class="info-b">
					<view class="wallet">
						<view class="wallet_info">
							<view class="info-item">
								<text class="num">{{info.balance_1 || 0}}</text>
								<text>积分</text>
							</view>
							<view class="info-item">
								<text class="num">{{order.TotalOrders || 0}}</text>
								<text>累计订单</text>
							</view>
							<view class="info-item">
								<text class="num">{{info.balance_4 || 0}}</text>
								<text>余额</text>
							</view>
							<view class="info-item" @click="toPages('/pages/coupon/quan')">
								<text class="num">{{info.quan_balance || 0}}</text>
								<text>优惠券</text>
							</view>

						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="showLogin" v-if="show_login" @click="show_login=false">
			<view class="bg" @click.stop>
				<view class="title">
					<image src="/static/logo.png" mode=""></image>
					新雅行
				</view>
				<view class="h1">
					登录后体验更多服务
				</view>
				<view class="ra">
					<label class="radio" @click="changeRadio">
						<radio :checked="is_agree" />
						<view class="radiotext">已阅读并同意<text @click="toPage('./xieyi')">《用户隐私协议》</text></view>
					</label>
				</view>
				<view class="btns">
					<!-- #ifdef H5 -->
					<button @click="toLogin" class="btn">立即登录</button>
					<!-- #endif -->
					<!-- #ifdef MP-WEIXIN -->
					<button v-if="is_agree" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"
						class="btn">立即登录</button>
					<button v-else @click="showTips" class="btn">立即登录</button>
					<!-- #endif -->
					<text class="out" @click="show_login=false">暂不登录</text>
				</view>
			</view>
		</view>
		<view class="order">
			<view class="title">
				<text class="h3">我的订单</text>
				<text class="all" @click="toPages('/pages/order/index?id=0')">更多 ></text>
			</view>
			<view class="tips">
				<view class="tips-item" v-for="(item,index) in tipslist"
					@click="toPages('/pages/order/index?id='+item.id)" :key='index'>
					<image :src="item.img" mode=""></image>
					<text>{{item.name}}</text>
					<text v-if="item.num>0" class="num">{{item.num}}</text>
				</view>
			</view>
		</view>
		<view class="order sever">
			<view class="title">
				<text class="h3">新雅行</text>
			</view>
			<view class="tips">
				<view class="tips-item" v-for="(item,index) in tagList" :key="index" @click="toPage(item.url)">
					<image :src="item.img" mode="widthFix"></image>
					<text>{{item.name}}</text>
				</view>
			</view>
		</view>
		<view class="order sever">
			<view class="title">
				<text class="h3">快捷工具</text>
				<!-- <text class="all" >全部></text> -->
			</view>
			<view class="tips">

				<view class="tips-item" @click="toPages('/pages/childshop/recharge')" v-if="info.type == 3">
					<uni-icons type="cloud-upload" size="28"></uni-icons>
					<text>充值管理</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/paycode/index')" v-if="info.type == 3 ">
					<uni-icons type="shop" size="28"></uni-icons>
					<text>子店收款码</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/paycode/index')" v-if="info.is_shop == 1">
					<uni-icons type="shop" size="28"></uni-icons>
					<text>分公司收款码</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/childshop/index')" v-if="info.is_shop == 1">
					<uni-icons type="shop" size="28"></uni-icons>
					<text>子店管理</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/myshop/index')"
					v-if="info.is_shop != 0 || info.type != 1">
					<uni-icons type="shop" size="28"></uni-icons>
					<text>我的店铺</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/myshop/withdraw')" v-if="info.is_shop == 1">
					<uni-icons type="staff" size="28"></uni-icons>
					<text>下级提现</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/finance/index')">
					<uni-icons type="list" size="28"></uni-icons>
					<text>账户明细</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/address/address')">
					<uni-icons type="location" size="28"></uni-icons>
					<text>收货地址</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/paycode/shop_recharge')" v-if="info.type == 3">
					<uni-icons type="auth" size="28"></uni-icons>
					<text>店长充值</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/childshop/sonrecharge')" v-if="info.is_shop == 1">
					<uni-icons type="auth" size="28"></uni-icons>
					<text>店长充值管理</text>
				</view>
				<view class="tips-item" @click="toPages('/pages/childshop/sonorder')" v-if="info.type == 3">
					<uni-icons type="list" size="28"></uni-icons>
					<text>查看订单</text>
				</view>
				<!-- <view class="tips-item" @click="toPages('/pages/register/childshop')">
					<uni-icons type="gift" size="28"></uni-icons>
					<text>切换子店</text>
				</view> -->

				<view class="tips-item" @click="toPages('/pages/usersetup/index')">
					<uni-icons type="person" size="28"></uni-icons>
					<text>个人资料</text>
				</view>

				<view class="tips-item" @click="toPages('/pages/coupon/list')">
					<uni-icons type="link" size="28"></uni-icons>
					<text>我的优惠券</text>
				</view>
				<!-- <view class="tips-item" @click="toPages('/pages/vip/turn')" v-if="info.type != 1">
					<uni-icons type="settings" size="28"></uni-icons>
					<text>积分转余额</text>
				</view> -->
				<!-- <view class="tips-item" @click="toPages('/pages/code/index')">
					<uni-icons type="scan" size="28"></uni-icons>
					<text>我的二维码</text>
				</view> -->


				<view class="tips-item" @click="outLogin" v-if="is_login">
					<uni-icons type="redo" size="28"></uni-icons>
					<text>退出登录</text>
				</view>

			</view>
		</view>

	</view>
</template>

<script>
	import {
		UserInfo,
		getUserInfo,
		UserIndex,
		GetOpenid,
		GetPhone,
		register
	} from "@/api/api.js"
	import uniCopy from '@/components/js_sdk/xb-copy/uni-copy.js'
	export default {
		data() {
			return {
				info: [],
				incomeData: [],
				isshow: false,
				is_login: false,
				tipslist: [{
						'id': 1,
						'name': '待付款',
						'img': '/static/icon/qianbao.png',
						num: 0
					},
					{
						'id': 2,
						'name': '待发货',
						'img': '/static/icon/dangan.png',
						num: 0
					},
					{
						'id': 3,
						'name': '待收货',
						'img': '/static/icon/daishouhuo.png',
						num: 0
					},
					{
						'id': 4,
						'name': '已完成',
						'img': '/static/icon/daka.png',
						num: 0
					},
					// {
					// 	'id': 5,
					// 	'name': '售后',
					// 	'img': '/static/index/order5.png'
					// },

				],
				open_id: '',
				TotalGet: 0,
				TotalOrders: 0,
				show_login: false,
				is_agree: false,
				indexInfo: [],
				order: [],
				tagList: [{
						name: '新雅行商城',
						img: '/static/icon/s5.png',
						url: '/pages/classlist/goods?type=4'

					},
					{
						name: '优惠券商城',
						img: '/static/icon/s7.png',
						url: '/pages/classlist/goods?type=2'
					},
					{
						name: '联系店长',
						img: '/static/icon/s6.png',
						url: '/pages/contact/index'
					},
					{
						name: '充值',
						img: '/static/icon/s8.png',
						url: '/pages/paycode/show'
					},
				],
				is_com: 0
			}
		},
		onLoad() {
			this.getMpOpenid()
		},
		onShow() {

			this.UserInfo()
			this.userIndex()
			this.show_login = false
		},
		methods: {
			showTips() {
				uni.$showMsg('请阅读并同意相关协议')
			},
			toLogin() {
				if (!this.is_agree) {
					uni.$showMsg('请阅读并同意相关协议')
				} else {
					uni.navigateTo({
						url: '/pages/login/index'
					})
				}
			},
			changeRadio(e) {
				this.is_agree = !this.is_agree
			},
			showLogin() {
				if (!this.is_login) {
					this.show_login = true
				}
			},
			updateHead() {
				if (!this.is_login) {
					return
				}
				let that = this
				uni.chooseImage({
					sourceType: ['album', 'camera'], //从相册选择
					count: 1,
					success: (res) => {
						let igmFile = res.tempFilePaths;
						uni.showLoading({
							mask: true,
							title: '加载中'
						})
						for (let i = 0; i < igmFile.length; i++) {
							uni.uploadFile({
								url: that.$config.requestUrl +
									'/User/forUpload',
								header: {
									'token': uni.getStorageSync('token')
								},
								formData: {
									accept: 'image',
								},
								filePath: igmFile[i],
								name: 'image',
								success: (res) => {
									uni.hideLoading()
									let imgRes = JSON.parse(res.data); //微信和头条支持
									if (imgRes.status == 1) {
										let imgData = imgRes.data
										that.info.head_url = that.$config.websiteUrl + imgData
									} else {
										uni.showToast({
											title: res.message,
											icon: "none"
										})
										return
									}
								},
								fail(err) {
									uni.hideLoading()
								}
							})
						}
					},
				});
			},
			userIndex() {
				UserIndex().then(res => {
					if (res.status == 1) {
						this.indexInfo = res.data.UserInfo
						this.order = res.data
						this.tipslist[0].num = res.data.OrderList_0
						this.tipslist[1].num = res.data.OrderList_1
						this.tipslist[2].num = res.data.OrderList_2
						this.tipslist[3].num = res.data.OrderList_3
						this.is_com = res.data.is_com
					} else {
						this.indexInfo = []
						this.order = []
						this.tipslist[0].num = 0
						this.tipslist[1].num = 0
						this.tipslist[2].num = 0
						this.tipslist[3].num = 0
					}
				})
			},
			getMpOpenid() {
				var that = this
				// #ifdef MP-WEIXIN
				uni.login({
					success(res) {
						GetOpenid({
							code: res.code
						}).then(data => {
							that.open_id = data.data
						})
					}
				})
				// #endif
			},
			getPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					let encryptedData = e.detail.encryptedData
					let errMsg = e.detail.errMsg
					let iv = e.detail.iv
					let code = e.detail.code
					GetPhone({
						openid: this.open_id,
						code
					}).then(res => {
						this.show_login = false
						if (res.status == 1) {
							let data = res.data
							register({
								openid: this.open_id,
								type: 1,
								shop_code: '',
								phone: data
							}).then(res => {
								if (res.status == 1 || res.status == 2) {
									setTimeout(() => {
										uni.setStorageSync('token', res.data)
										this.UserInfo()
										this.userIndex()
									})
								} else {
									uni.$showMsg(res.message)
								}
							})
						} else if (res.status == 2) {
							uni.setStorageSync('token', res.data)
							this.UserInfo()
							this.userIndex()
						}
					})
				}
			},
			UserInfo() {
				UserInfo().then(res => {
					if (res.status == 1) {
						if (res.data) {
							this.info = res.data
							if (this.info.head_url) {
								this.info.head_url = this.$config.websiteUrl + this.info.head_url
							}
							this.is_login = true
						} else {
							this.is_login = false
						}
					} else {
						this.info = ''
						this.is_login = false
					}
				})
			},
			outLogin() {
				var that = this
				uni.showModal({
					title: '提示',
					content: '确认退出登录?',
					success(res) {
						if (res.confirm) {
							uni.removeStorageSync('token')
							that.UserInfo()
							this.is_login = false
						}
					}
				})
			},
			changeShow() {
				this.isshow = !this.isshow
			},
			copy() {
				uniCopy({
					content: this.info.recommend_code,
					success: (res) => {
						uni.showToast({
							title: res,
							icon: 'none'
						})
					},
					error: (e) => {
						uni.showToast({
							title: e,
							icon: 'none',
							duration: 3000,
						})
					}
				})
			},
			toPages(url) {
				if (!this.is_login) {
					uni.$showMsg('请先登录')

				} else {
					uni.navigateTo({
						url: url
					})
				}

			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F5F5;
		min-height: 100vh;
		/* #ifdef H5 */
		min-height: calc(100vh - 94px);
		/* #endif */
	}

	.showLogin {
		position: fixed;
		top: 0;
		left: 0;
		width: 100vw;
		height: 100vh;
		background: #000000c0;
		z-index: 999;

		.bg {
			background: #fff;
			border-top-left-radius: 18px;
			border-top-right-radius: 18px;
			padding: 15px;
			box-sizing: border-box;
			width: 100%;
			position: absolute;
			bottom: 0;

			.title {
				display: flex;
				align-items: center;
				font-weight: bold;
				font-size: 17px;
				margin-top: 10px;

				image {
					width: 25px;
					height: 25px;
					margin-right: 5px;
					border-radius: 50%;
				}
			}

			.h1 {
				font-size: 18px;
				font-weight: bold;
				margin-top: 20px;
			}

			.ra {
				margin-top: 15px;

				label {
					display: flex;
					align-items: center;

					.radiotext {
						text {
							color: #FF3C29;
						}
					}
				}
			}

			.btns {
				display: flex;
				align-items: center;
				flex-direction: column;
				justify-content: center;
				margin-top: 30px;
				color: #777;

				.btn {
					width: 96%;
					font-size: 14px;
					border-radius: 20px;
				}
			}
		}
	}

	.top {
		position: relative;
		height: 180px;
		overflow: hidden;

		.topbg {
			width: 100%;
		}

		.infobox {
			width: 100%;
			position: absolute;
			top: 0;
			padding-top: 10px;

			.info-t {
				padding-top: 10px;
				display: flex;
				padding-left: 5%;
			}
		}

		.header_logo {
			height: 60px;
			width: 60px;
			border-radius: 50%;
		}

		.userinfo {
			color: #000;
			flex: 1;
			margin-left: 3%;
			// background: url(@/static/index/tiaowen.png);
			background-size: 100% 100%;

			.title {
				display: flex;
				align-items: center;
				background: transparent;
				font-size: 14px;
				color: #000;
				margin: 0;
				padding: 0;
				text-align: left;
				justify-content: space-between;
				line-height: 20px;

				&::after {
					border: 0;
				}

				image {
					width: 20px;
					height: 20px;
				}

				.name {
					display: block;
					width: 50%;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.level {
					position: relative;
					line-height: 20px;
					padding-right: 5%;
					z-index: 99;
				}

				.share {
					margin-left: 15%;
				}
			}

			.phone {
				font-size: 24rpx;
				margin-top: 3px;
			}

			.code {
				display: flex;
				align-items: center;

				.code-text {
					font-size: 13px;
					max-width: 55%;
					display: block;
				}

				.copy {
					background: #F1D3A8;
					color: #6C2A09;
					padding: 1% 3%;
					border-radius: 5px;
					font-size: 13px;
					margin-left: 5%;
				}

				image {
					width: 16px;
					height: 13px;
					margin-left: 10%;
				}
			}
		}

	}

	.wallet {
		width: 96%;
		margin: 0 auto;
		color: #000;
		position: relative;

		.tixian {
			padding-top: 30px;
			padding-left: 5%;

			text {
				font-size: 22px;
			}
		}

		.tixian_now {
			position: absolute;
			right: 5%;
			top: 15%;
			border-radius: 20px;
			background: #ffffffb3;
			color: #C6A06F;
			padding: 1% 4%;
			font-size: 14px;
		}

		.wallet_info {
			display: flex;
			justify-content: space-around;
			text-align: center;
			margin-top: 20px;

			view {
				flex: 1;
				display: flex;
				flex-direction: column;
				font-size: 12px;

				.num {
					font-weight: bold;
					font-size: 16px;
					margin-bottom: 5px;
				}
			}
		}
	}

	.order {
		padding: 0 10px;
		background: #fff;
		padding-bottom: 40rpx;
		width: 96%;
		box-sizing: border-box;
		margin: 0 auto;
		margin-bottom: 30px;
		margin-top: -20px;
		border-radius: 8px;
		z-index: 87;
		position: relative;

		.title {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 5% 0;
			border-bottom: 1px solid #efefef;

			.h3 {
				font-weight: bold;
				color: #000;
				font-size: 17px;
			}

			.all {
				color: #999999;
				font-size: 14px;
			}
		}

		.tips {
			display: flex;
			justify-content: space-around;
			margin-top: 30rpx;

			.tips-item {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				position: relative;

				.num {
					background: #FF3C29;
					color: #fff;
					font-size: 12px;
					border-radius: 50%;
					width: 20px;
					height: 20px;
					top: -10px;
					right: 0;
					display: flex;
					justify-content: center;
					align-items: center;
					position: absolute;
					transform: scale(.8);
				}

				image {
					width: 24px;
					height: 24px;
				}

				text {
					font-size: 13px;
					margin-top: 10rpx;
					color: #777777;
				}
			}
		}
	}

	.sever {
		padding-bottom: 10px;

		.tips {
			display: table;
			width: 100%;

			.tips-item {
				float: left;
				width: 25%;
				margin-bottom: 30rpx;

				image {
					width: 50px;
					height: 50px;
				}
			}
		}
	}
</style>